﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.IO;
using System.Reflection;

namespace JustAMinute
{
    public partial class GameForm : Form
    {
        private int time;
        private Engine engine;
        private Graphic graphic;
        private SoundPlayer timerSound;
        private SoundPlayer gameOverSound;
        private Level currentLevel;

        public GameForm(Level level)
        {
            InitializeComponent();

            time = level.Time;

            this.currentLevel = level;

            this.BackColor = level.Color;
            this.Text = "Just A Minute";

            this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;

            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            timerSound = new SoundPlayer(Path.Combine(directory, "DataStore/Sounds/tick.wav"));
            gameOverSound = new SoundPlayer(Path.Combine(directory, "DataStore/Sounds/end.wav"));

            gameTimer.Interval = 100;
            secondGameTimer.Interval = 1000;
            checkingTimer.Interval = 1200;

            engine = new Engine(level, this);
            engine.CreateCards();
            graphic = new Graphic(engine.ListOfCards);
            graphic.Display(this);
            graphic.CheckCardsEvent += engine.CheckIsPair;
            

            checkingTimer.Tick += graphic.CheckCards;
            secondGameTimer.Tick += TimerTick;
            gameTimer.Tick += ShowHideFormBorders;
            gameTimer.Tick += CheckTimer;

            timeValueLabel.Text = time.ToString();
            pointsValueLabel.Text = engine.Points.ToString();
            levelLabel.Text += engine.LevelName;

        }

        private void GameForm_Load(object sender, EventArgs e)
        {
            gameTimer.Start();
            secondGameTimer.Start();
            checkingTimer.Start();
  
        }

        private void TimerTick(object sender, EventArgs e)
        {
            if (time >= 0 && engine.CardsCount !=0)
            {
                timeValueLabel.Text = (time--).ToString();
                pointsValueLabel.Text = engine.Points.ToString();

                if (time <= 10)
                {
                    timerSound.Play();
                }
               
            }

            else
            {
                GameOverForm gameOverForm;
             
                string header;
                string message;
                secondGameTimer.Stop();
                gameOverSound.Play();

                if(engine.Points != 240)
                {
                    header = "Game  is  over!  Your time is out!";
                    message = "You have got  " + engine.Points + " points.";    
                }

                else
                {
                    header = "Congritulations! You win!";
                    message = "You have got  " + engine.Points + " points.";
                }

                gameOverForm = new GameOverForm(header, message);
                gameOverForm.Owner = this;
                gameOverForm.Show();
            }
           
        }

        private void CheckTimer(object sender, EventArgs e)
        {
            if(secondGameTimer.Enabled)
            {
                graphic.SetCardsAble(true);
            }
            else
            {
                graphic.SetCardsAble(false);
            }
        }

        private void ShowHideFormBorders(object sender, EventArgs e)
        {

            int yPosition = Cursor.Position.Y;

            if (yPosition > 20)
            {
                this.FormBorderStyle = FormBorderStyle.None;
            }

            else
            {
                this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            }
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            if (secondGameTimer.Enabled)
            {
                secondGameTimer.Enabled = false;
                pauseButton.Text = "Resume";
            }
            else
            {
                secondGameTimer.Enabled = true;
                pauseButton.Text = "Pause";
            }
        }

        private void menuButton_Click(object sender, EventArgs e)
        {
            this.Hide();
            pauseButton_Click(sender, e);
            
        }

        
    }
}
